#include "cslepui.h"
#include "cslep.h"
#include "slepbase.h"
#include <QtGui>



void CSLEPUI::actglLeastR(){
	optsWidget = new SlepOptionsWidget(Method_glLeast_R);
	LogWidget::instance()->log(QString("Executing Group Lasso"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		SLEPItem *itemA = SLEPFileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = SLEPFileOperations::slepVector2SlepItem(y);
        if(itemA->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("A");
			SLEPFileOperations::writeTextFile(*itemA,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemA);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemA->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemy->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("y");
			SLEPFileOperations::writeTextFile(*itemy,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemy);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemy->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

		glLeastR(A,y,optsWidget->getL1RegCoefficient(),opts,x,funVal,valueL);
		LogWidget::instance()->log(QString("Success executing Group Lasso"));


		SLEPItem *itemx = SLEPFileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = SLEPFileOperations::slepVector2SlepItem(funVal);


        if(itemx->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("x");
			SLEPFileOperations::writeTextFile(*itemx,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemx);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemx->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
        if(itemFVal->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("fVal");
			SLEPFileOperations::writeTextFile(*itemFVal,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemFVal);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemFVal->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}


void CSLEPUI::actglLogisticR(){
	optsWidget = new SlepOptionsWidget(Method_glLogistic_R);
	double c;
	LogWidget::instance()->log(QString("Group Lasso with Logistic Loss - Options"));
	int success = optsWidget->exec();
	if(success == QDialog::Accepted){
		OPTS *opts = optsWidget->getOPTS();
		slep_matrix* A = optsWidget->getInputA();
		slep_vector* y = optsWidget->getInputy();
		slep_vector* x = slepAllocDenseVector(A->n);
		slep_vector* funVal = slepAllocDenseVector(opts->maxIter);
		slep_vector* valueL = slepAllocDenseVector(opts->maxIter);

		SLEPItem *itemA = SLEPFileOperations::slepMatrix2SlepItem(A);
		SLEPItem *itemy = SLEPFileOperations::slepVector2SlepItem(y);
        if(itemA->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("A");
			SLEPFileOperations::writeTextFile(*itemA,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemA);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemA->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

        if(itemy->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("y");
			SLEPFileOperations::writeTextFile(*itemy,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemy);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemy->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}

		stopWatch timer;
		LogWidget::instance()->log(QString("Now running..."));
		::startTimer(&timer);
		glLogisticR(A,y,optsWidget->getL1RegCoefficient(),opts,x,&c,funVal,valueL);
		::stopTimer(&timer);
		LogWidget::instance()->log(QString("It took %1 seconds.").arg(getElapsedTime(&timer)));
		LogWidget::instance()->log(QString("Success executing Group Lasso with Logistic Loss!"));


		SLEPItem *itemx = SLEPFileOperations::slepVector2SlepItem(x);
		SLEPItem *itemFVal = SLEPFileOperations::slepVector2SlepItem(funVal);

		{
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			SLEPItem itemC;
			itemC.singlevalue=c;
			itemC.type=SLEPItem::SINGLE_VALUE;
            itemModel->insertRow(0);
            slepItemList->insert(0,itemC);
            itemModel->setData(itemModel->index(0,0),"c");
            itemModel->setData(itemModel->index(0,1),"Scalar");
            itemModel->setData(itemModel->index(0,2),c);
			itemModel->setData(itemModel->index(0,3),"");
		}

        if(itemx->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("x");
			SLEPFileOperations::writeTextFile(*itemx,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemx);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemx->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
        if(itemFVal->type != SLEPItem::NONE){
			QStandardItemModel* itemModel = fileTable->getItemModel();
			QList<SLEPItem>* slepItemList = fileTable->getSlepItemList();
			QString fileName("fVal");
			SLEPFileOperations::writeTextFile(*itemFVal,fileName);
            itemModel->insertRow(0);
            slepItemList->insert(0,*itemFVal);
            itemModel->setData(itemModel->index(0,0),QFileInfo(fileName).baseName());
            itemModel->setData(itemModel->index(0,1),QString(SLEPItem::TYPES_LONG[slepItemList->at(0).type]));
            itemModel->setData(itemModel->index(0,2),itemFVal->info());
			itemModel->setData(itemModel->index(0,3),QFileInfo(fileName).canonicalFilePath());
		}
		slepFreeMatrix(A);
		slepFreeVector(x);
		slepFreeVector(y);
		slepFreeVector(funVal);
		slepFreeVector(valueL);
		QMessageBox::information(this,tr("Success"), tr("It ran successfully!"));
	}
	else{
		QMessageBox::information(this,tr("Error"), tr("Was not able to execute as there were errors in input!"));
	}
	delete optsWidget;
}